const BASE_URL = 'http://localhost:8081/api'

// 通用请求函数
async function request(endpoint, options = {}) {
    const url = `${BASE_URL}${endpoint}`
    const config = {
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        },
        ...options
    }

    if (config.body && typeof config.body === 'object') {
        config.body = JSON.stringify(config.body)
    }

    try {
        const response = await fetch(url, config)
        const data = await response.json()

        if (!response.ok) {
            throw new Error(data.message || '请求失败')
        }

        return data
    } catch (error) {
        console.error('API请求错误:', error)
        throw error
    }
}

// 用户相关API
export const userApi = {
    login: (mobile, password) => request('/users/login', {
        method: 'POST',
        body: { mobile, password }
    }),
    register: (userData) => request('/users/register', {
        method: 'POST',
        body: userData
    }),
    getUser: (userId) => request(`/users/${userId}`),
    updateNickname: (userId, nickname) => request(`/users/${userId}/nickname`, {
        method: 'PUT',
        body: { nickname }
    })
}

// 课程相关API
export const courseApi = {
    getAvailableCourses: () => request('/courses/available'),
    getCourseById: (courseId) => request(`/courses/${courseId}`),
    createCourse: (courseData) => request('/courses', {
        method: 'POST',
        body: courseData
    }),
    updateCourse: (courseId, courseData) => request(`/courses/${courseId}`, {
        method: 'PUT',
        body: courseData
    })
}

// 预约相关API
export const reservationApi = {
    getUserReservations: (userId) => request(`/reservations/user/${userId}`),
    createReservation: (reservationData) => request('/reservations/create', {
        method: 'POST',
        body: reservationData
    }),
    cancelReservation: (reservationId, reason) => request(`/reservations/${reservationId}/cancel`, {
        method: 'PUT',
        body: { reason }
    })
}

// 公告相关API
export const announcementApi = {
    getAll: () => request('/announcements/all'),
    getById: (id) => request(`/announcements/${id}`),
    create: (announcementData) => request('/announcements/insert', {
        method: 'POST',
        body: announcementData
    }),
    update: (announcementData) => request('/announcements/update', {
        method: 'PUT',
        body: announcementData
    }),
    publish: (id) => request(`/announcements/${id}/publish`, {
        method: 'PUT'
    }),
    withdraw: (id) => request(`/announcements/${id}/withdraw`, {
        method: 'PUT'
    }),
    delete: (id) => request(`/announcements/${id}`, {
        method: 'DELETE'
    })
}

// 教练相关API
export const coachApi = {
    getAll: () => request('/coaches/all'),
    getById: (id) => request(`/coaches/${id}`),
    create: (coachData) => request('/coaches/insert', {
        method: 'POST',
        body: coachData
    }),
    update: (coachData) => request('/coaches/update', {
        method: 'PUT',
        body: coachData
    }),
    delete: (id) => request(`/coaches/${id}`, {
        method: 'DELETE'
    })
}

// 会员卡相关API
export const memberCardApi = {
    getAll: () => request('/member-card-type/list'),
    getAvailable: () => request('/member-card-type/available'),
    create: (cardData) => request('/member-card-type', {
        method: 'POST',
        body: cardData
    }),
    update: (cardData) => request('/member-card-type/update', {
        method: 'PUT',
        body: cardData
    }),
    delete: (id) => request(`/member-card-type/${id}`, {
        method: 'DELETE'
    })
}